# coding: utf-8

from config import app
from const import BorrowCardApplyStatus
from datetime import datetime

def gender_filter(gender):
    if gender == 0:
        return '男'
    elif gender == 1:
        return '女'
    else:
        return '未知'


def is_active_filter(is_active):
    if is_active == 0:
        return '否'
    elif is_active == 1:
        return '是'
    else:
        return '未知'

def yes_no_filter(yes_no):
    if yes_no == 0:
        return '否'
    elif yes_no == 1:
        return '是'
    else:
        return '未知'

def borrow_card_apply_status_filter(borrow_card_apply_status):
    if borrow_card_apply_status == 0:
        return BorrowCardApplyStatus.UNAPPLY.name
    elif borrow_card_apply_status == 1:
        return BorrowCardApplyStatus.APPLYING.name
    elif borrow_card_apply_status == 2:
        return BorrowCardApplyStatus.APPLIED.name
    elif borrow_card_apply_status == -1:
        return BorrowCardApplyStatus.FAILED.name
    else:
        return '未知'

def borrow_days_filter(borrow_time):
    days = (datetime.now() - borrow_time).days
    return f"{days}天"

def return_days_filter(return_time):
    days = (return_time - datetime.now()).days
    return f"{days}天"

def init_filters():
    app.jinja_env.filters['gender_filter'] = gender_filter
    app.jinja_env.filters['is_active_filter'] = is_active_filter
    app.jinja_env.filters['yes_no_filter'] = yes_no_filter
    app.jinja_env.filters['borrow_card_apply_status_filter'] = borrow_card_apply_status_filter
    app.jinja_env.filters['borrow_days_filter'] = borrow_days_filter
    app.jinja_env.filters['return_days_filter'] = return_days_filter